package jeconbond.economic.agent;

import java.util.ArrayList;
import java.util.List;

import jeconbond.automata.INode;
import jeconbond.automata.behaviour.IAutomataWalkListener;

public class AutomataActivityListener implements IAutomataWalkListener {
	private int[] stepsCounter_ptr;
	private int activityCount = 0;
	private int totalCounter = 0;
	private int prevStepsCounter = 0;
	public List<Double> activityTrack = new ArrayList<Double>();

	public AutomataActivityListener(int[] stepsCounter_ptr) {
		this.stepsCounter_ptr = stepsCounter_ptr;
	}

	@Override
	public void onWalkingPerformed(INode from, INode to) {
		checkIfWrite();
		activityCount++;
		totalCounter++;
	}

	@Override
	public void sameState(INode state) {
		checkIfWrite();
		totalCounter++;
	}

	private void checkIfWrite() {
		if (stepsCounter_ptr[0] > prevStepsCounter) {
			writeNote();
		}
		prevStepsCounter = stepsCounter_ptr[0];
	}

	private void writeNote() {
		double activity = 100.0 * activityCount / (double)totalCounter;
		activityTrack.add(activity);
		activityCount = 0;
		totalCounter = 0;
	}

	@Override
	public List<Double> getReport() {
		writeNote();
		List<Double> result = activityTrack;
		activityTrack = null; // invalidate object
		return result;
	}
}
